package com.example.idandiclibrary.IDandICHelp.IDAndICUtils;

import android.content.Context;
import android.util.Log;

import com.example.baselibrary.Utils.LogUtil;
import com.example.idandiclibrary.IDandICHelp.Interface.IDAndICInterface;
import com.zkteco.android.biometric.core.device.ParameterHelper;
import com.zkteco.android.biometric.core.device.TransportType;
import com.zkteco.android.biometric.core.utils.LogHelper;
import com.zkteco.android.biometric.core.utils.ToolUtils;
import com.zkteco.android.biometric.module.idcard.IDCardReader;
import com.zkteco.android.biometric.module.idcard.IDCardReaderFactory;
import com.zkteco.android.biometric.module.idcard.exception.IDCardReaderException;
import com.zkteco.android.biometric.module.idcard.meta.IDCardInfo;

import java.util.HashMap;
import java.util.Map;

public class IDAndICHelper {

    private static final String idSerialName = "/dev/ttyS2";
    private static final int idBaudrate = 115200;
    private static IDCardReader idCardReader = null;
    private static boolean isOpen = false;
    private static int index = 0;
    private static IDAndICInterface idAndICInterface;

    public static void OpenIDAndICard(Context context) {
        try {
            startIDCardReader(context);
            idCardReader.open(0);
            isOpen = true;
            ReadIDCard();
            ReadICCard();
        } catch (IDCardReaderException e) {
            e.printStackTrace();
        }
    }

    public static void ReadIDCard() {
        if (!isOpen) {
            LogUtil.v("Id和Ic卡没打开，请先去打开");
            return;
        }
//        LogUtil.v("我再读卡 你说呢 ReadIDCard");
        Authenticate();
        IDCardInfo idCardInfo = new IDCardInfo();
        try {
            boolean ret = idCardReader.readCard(0, 1, idCardInfo);
            if (ret) {
//                LogUtil.v("姓名：" + idCardInfo.getName() + "，民族：" + idCardInfo.getNation() + "，住址：" + idCardInfo.getAddress() + ",身份证号：" + idCardInfo.getId());
                idAndICInterface.showIDMessage( idCardInfo.getId());
            }
        } catch (IDCardReaderException e) {
            e.printStackTrace();
//            LogUtil.v("读卡失败, 错误码：" + e.getErrorCode() + "\n错误信息：" + e.getMessage() + "\n 内部错误码=" + e.getInternalErrorCode());
        }

    }

    public static void ReadICCard() {
        if (!isOpen) {
            LogUtil.v("Id和Ic卡没打开，请先去打开");
            return;
        }

        byte mode = (byte) 0x01;//写操作模式0或1
        byte blockCount = (byte) 0x01;//要读多少块1-4
        byte startAddress = (byte) 0x10;//16进制0x00-0x3F 即0到63块
        byte[] key = new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};//6字节秘钥

        byte[] dataCard = new byte[16];//读出的卡数据
        byte[] cardNum = new byte[4];//读出卡序列号
        boolean ret = false;
        try {
            ret = idCardReader.MF_Read(index, mode, blockCount, startAddress, key, cardNum, dataCard);
            if (ret) {
                LogUtil.v("cardNum:" + ToolUtils.bytesToHexString(cardNum) + "     " + "dataCard:" + ToolUtils.bytesToHexString(dataCard));
                idAndICInterface.showICMessage(ToolUtils.bytesToHexString(cardNum));
            }
        } catch (IDCardReaderException e) {
            e.printStackTrace();
//            LogUtil.v("写操作失败, 错误码：" + e.getErrorCode() + "\n错误信息：" + e.getMessage() + "\n 内部错误码=" + e.getInternalErrorCode());
        }


    }


    private static void startIDCardReader(Context context) {
        // Define output log level
        LogHelper.setLevel(Log.VERBOSE);
        // Start fingerprint sensor
        Map idrparams = new HashMap();
        String strSerialName = "/dev/ttyS2";
        if (strSerialName != null && strSerialName.length() > 0) {
            idrparams.put(ParameterHelper.PARAM_SERIAL_SERIALNAME, strSerialName);
        } else {
            idrparams.put(ParameterHelper.PARAM_SERIAL_SERIALNAME, idSerialName);
        }

        idrparams.put(ParameterHelper.PARAM_SERIAL_BAUDRATE, idBaudrate);
        idCardReader = IDCardReaderFactory.createIDCardReader(context, TransportType.SERIALPORT, idrparams);

    }

    private static boolean Authenticate()   //放卡一直读取不判断该返回值继续读卡
    {
        try {
            idCardReader.findCard(0);
            idCardReader.selectCard(0);
            return true;
        } catch (IDCardReaderException e) {
            e.printStackTrace();
            return false;
        }
    }

    //关闭 卡读线程
    public static void CloseIDAndIC() {
        IDCardReaderFactory.destroy(idCardReader);
    }

    //回调 结果出去
    public static void setIDAndICListener(IDAndICInterface idAndICListeners) {
        idAndICInterface = idAndICListeners;
    }
}
